package com.park.web.websocket;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.park.client.model.user.UserModel;

public class MessageHandShakeInterceptor implements HandshakeInterceptor{

    private  static final Log log=LogFactory.getLog(MessageHandShakeInterceptor.class);

	@Override
	public void afterHandshake(ServerHttpRequest arg0, ServerHttpResponse arg1, WebSocketHandler handler, Exception exceptions) {
        log.info("=================执行 afterHandshake ：handler: "+handler+"exceptions: "+exceptions);
	}

	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse arg1, WebSocketHandler handler,
			Map<String, Object> map) throws Exception {
        if(request instanceof ServerHttpRequest){
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            HttpSession session = servletRequest.getServletRequest().getSession();
            if(session != null){
                UserModel user=(UserModel)session.getAttribute("user");
                map.put(user.getUsername(), user);
            }
        }
        return true;
	}

}
